package com.lianqi.emcpframework.design.configuration;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.context.ApplicationEventPublisherAware;

import com.lianqi.emcpframework.design.domain.event.DomainEventBus;

/**
 * @program emcp-framework
 * @ClassName SpringDomainEventDispatcher
 * @description: 领域事件分发类
 * @author: sky
 * @create: 2019/06/06 20:07
 */
public class SpringDomainEventDispatcher implements ApplicationEventPublisherAware {

    @Autowired
    private DomainEventBus domainEventBus;

    ApplicationEventPublisher eventPublisher;

    @Override
    public void setApplicationEventPublisher(ApplicationEventPublisher applicationEventPublisher) {
        this.eventPublisher = applicationEventPublisher;
    }

    /**
     * 发布领域事件
     */
    public void addDomainEventListener() {
        this.domainEventBus.register(event -> true, event -> {
            this.eventPublisher.publishEvent(event);
        });
    }
}